;; osc-responder.scm - (c) rohan drape, 2003-2005

;; Instances of <osc-transport> route all incoming OSC packets through
;; osc-responder-dispatch.

;; The global OSC table of registered OSC responders.  It is keyed on
;; a <string> command.

(define-global osc-responder-table (make-table))

;; Register the three argument procedure `responder' to receive
;; incoming OSC packets that match `command'.  The OSC command must be
;; specified as a string.  The argument to `responder' is the lisp
;; representation of the OSC message.  If there is an existing
;; responder it is overwritten.

(define (osc-responder-register command responder)
  (if (not (string? command))
      (error "osc-responder-register: illegal command" command))
  (table-put! (osc-responder-table) 
	      command
	      responder))

;; Remove any registered reponder at `command'.

(define (osc-responder-unregister command)
  (table-remove! (osc-responder-table) command))

;; Clear all entries from the responder table.

(define (osc-responder-clear)
  (table-clear! (osc-responder-table)))

;; The dispatch procedure that responders all incoming OSC byte
;; packets and calls the appropriate registered responders..

(define (osc-responder-dispatch packet)
  (let* ((lisp-packet (osc-decode-packet packet))
	 (entry (table-get* (osc-responder-table)
			    (car lisp-packet))))
    (if entry (entry lisp-packet))))

